// Copyright (c) 2022 Presto Labs Pte. Ltd.
// Author: juntang

#pragma once

#include <cstdio>
#include <map>
#include <memory>
#include <set>
#include <string>

#include <rapidjson/document.h>

#include "coin/proto/coin_request.pb.h"
#include "coin2/exchange/base/order/order.h"
#include "coin2/exchange/bitget/symbology/product.h"
#include "coin2/exchange/bitget_base/api_util/rate_tracker.h"

namespace coin2::exchange::bitget::order_v1 {

class BitgetParser : public base::order::BaseParser {
 public:
  using TransferType = coin::proto::TransferRequestProto::TransferType;

 public:
  const ParseResult& ParseRawMessage(const RawMessage& msg) override;
  AccountBalance ParseBalance(const JsonValue& document) override;
  bool ParseRawBalance(const JsonValue& doc, RawBalanceMap* raw_balance_map);

 private:
  void ParseSubmitResponse();
  void ParseCancelResponse();
  void ParseRestOpenOrders();
  void ParseRestAllOrders();
  void ParseRestOrderInfoList();
  void ParseRestFillInfoList();
  void ParseRestSpotAccount();
  void ParseWsMessage();
  ProductOrderElement ParseRestOrderInfo(const JsonValue&);

  void MergeMarginAccountBalance(AccountBalance* acc_balance);
  bool IsAccountUpdate(const JsonDocument& json);
  bool IsOrderUpdate(const JsonDocument& json);

  void ParseWsOrderInfoList(const JsonDocument& document);
  void ParseWsFillInfoList(const JsonDocument& document);  
  void ParseWsAccountInfoList(const JsonDocument& document);
};

}  // namespace coin2::exchange::bitget::order_v1
